#!/bin/sh

set -e

########## 路径宏定义 ####################
ROOT_DIR=$(cd $(dirname $0); pwd)
SCRIPT_DIR=${ROOT_DIR}/script
TEXT_DIR=${ROOT_DIR}/text
########## 路径宏定义 ####################

source ${SCRIPT_DIR}/common.sh

function hb_install() {
    source ${SCRIPT_DIR}/hb_install.sh 
}

function create_log_file() {
    local log_file=${ROOT_DIR}/log
    if [ -e ${log_file} ]; then
        rm ${log_file}
    fi
    echo -e "create log file ${log_file}"
    touch ${log_file}
}

function check_sh() {
    local cnt=$(ls -l /bin/sh | grep dash | wc -l)
    if [[ cnt -gt 0 ]]; then
        echo -e "/bin/sh is link to /bin/dash, will to change to /bin/bash"
        bash -c "rm /bin/sh; ln -s /bin/bash /bin/sh"
    else
        echo -e "/bin/sh is link to /bin/bash"
    fi
}

function apt_get_install() {
    local apt_list=${TEXT_DIR}/apt_list.md
    if [ ! -f ${apt_list} ]; then
        echo -e "Please set list file dir"
        return 0
    fi

    while read install_cmd; do
        local cnt=$(echo "${install_cmd}" | grep apt | wc -l)
        if [[ ${cnt} -gt 0 ]]; then
            echo -e "${install_cmd}"
            bash -c "${install_cmd}"
        fi
    done < ${apt_list}
}

function repo_install() {
    curl -s https://gitee.com/oschina/repo/raw/fork_flow/repo-py3 > repo
    chmod a+x repo
    mv repo /usr/local/bin/
}

function git_lfs_install() {
    curl -s https://packagecloud.io/install/repositories/github/git-lfs/script.deb.sh | bash
    apt-get -y -f install git-lfs
    git lfs install
}

function git_config() {
    local user_name=$1
    local git_user_name=$2
    local git_email=$3
    bash -c "su - ${user_name} -s /bin/bash ${ROOT_DIR}/script/test.sh ${git_user_name} ${git_email}"
}

function change_sources_list() {
    local sources_file=${TEXT_DIR}/sources.list
    local current_file=/etc/apt/sources.list
    local list_bak=/etc/apt/sources.list_bak
    local md5file=${TEXT_DIR}/md5
    if [ ! -f ${sources_file} ]; then
        echo -e "${sources_file} is not exist"
        exit -1
    fi

    if [ ! -f ${current_file} ]; then
        echo -e "${current_file} is not exist, change new file"
        bash -c "cp ${sources_file} ${current_file}"
        bash -c "apt-get update"
        bash -c "apt-get upgrade -y"
        return 0
    fi

    bash -c "md5sum ${sources_file} ${current_file} > ${md5file}"
    ret=$(awk 'BEGIN {idx=0} {arr[idx]=$1; idx++} END {if (arr[0] == arr[1]) { print "OK" } else {print "FAILED"}}' ${md5file})
    bash -c "rm -rf ${md5file}"
    if [ ${ret} = "OK" ]; then
        echo "sources list is already change"
        return 0
    fi

    mv ${current_file} ${list_bak}
    bash -c "cp ${sources_file} ${current_file}"
    bash -c "apt-get update"
    bash -c "apt-get upgrade -y"
}

function openharmony_code_download() {
    mkdir -p ~/OpenHarmony/master
    cd ~/OpenHarmony/master
    repo init -u https://gitee.com/openharmony/manifest.git -b master --no-repo-verify
    repo sync -c
    repo forall -c 'git lfs pull'
    repo start master --all
    bash ./build/prebuilts_download.sh
}

function main() {
    # 检查当前是否为root账号
    check_root

    # 配置log
    # create_log_file

    # 更换安装源
    change_sources_list
    
    # 配置sh为bash
    check_sh

    # 文件格式全部转换为unix格式
    file_format_check

    # 安装常用工具包
    apt_get_install

    # git lfs安装
    git_lfs_install

    # 配置repo
    repo_install

    # 配置hb命令
    hb_install
}

main
